package main

import "fmt"

/**
切片使用
 */
func main() {

	s := []int{1, 2, 3}

	//for i, v := range s {
	//	fmt.Println("i", )
	//}

	printSlice(s)

	/** 空切片 **/
	var numbers []int
	if numbers == nil {
		fmt.Println("切片是空的")
	}

	/** 切片截取 **/
	number1 := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	printSlice(number1)

	fmt.Println("number1==", number1)

	fmt.Println("number1[1:4] == ", number1[1:4])

	fmt.Println("number1[:3] ==", number1[:3])

	fmt.Println("number1[4:]==", number1[4:])

	number2 := make([]int, 0, 5)
	printSlice(number2)

	/* 打印子切片从索引 0(包含) 到索引 2(不包含) */
	number3 := number1[:2]
	printSlice(number3)
	fmt.Println("number3==", number3)

	/* 打印子切片从索引 2(包含) 到索引 5(不包含) */
	number4 := number1[2:5]
	printSlice(number4)

	var nums []int //声明一个切片
	printSlice(nums)

	/** 允许追加空切片**/
	nums = append(nums, 0)
	printSlice(nums)

	/** 向切片添加一个元素 **/
	nums = append(nums, 1)
	printSlice(nums)

	/** 同时向切片添加多个元素 **/
	nums = append(nums, 2, 3, 4, 5)
	printSlice(nums)

	/**创建切片 nums1是之前切片nums的两倍容量 **/
	nums1 := make([]int, len(nums), cap(nums)*2)
	printSlice(nums1)

	/** 拷贝nums的内容到nums1 **/
	copy(nums1, nums)
	printSlice(nums1)
}

func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}
